package acwing.trie;

import java.util.Scanner;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/11/16
 */
public class trie {

    static int N = 100010;
    static int[][] son = new int[N][26];
    static int[]cnt = new int[N];
    static int idx;

    public static void insert(char[] str) {
        int p = 0;
        for (int i = 0; i < str.length; i++) {
            int u = str[i] - 'a';
            if (son[p][u] == 0) {
                son[p][u] = ++idx;
            }
            p = son[p][u];
        }
        cnt[p]++;
    }
    public static int query(char[] str) {
        int p = 0;
        for (int i = 0; i < str.length; i++) {
            int u = str[i] - 'a';
            if (son[p][u] == 0) {
                return 0;
            }
            p = son[p][u];
        }
        return cnt[p];
    }

    public static void main(String[] args) {
        Scanner sy = new Scanner(System.in);
        int m = sy.nextInt();
        while(m--!=0)
        {
            String op = sy.next();
            String str = sy.next();
            if(op.equals("I"))  insert(str.toCharArray());
            else    System.out.println(query(str.toCharArray()));
        }
    }

}
